package com.github.zy.rpc.controller;

import com.github.zy.rpc.client.test.TestServiceRequest;
import com.google.common.collect.Lists;
import com.github.zy.rpc.domain.MessageModule;
import com.github.zy.rpc.service.handler.ServerSendTemplate;
import com.github.zy.rpc.service.handler.ServerSender;
import com.github.zy.rpc.session.Session;
import com.github.zy.rpc.session.SessionManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;

/**
 * @version 1.0 created by zy on 2020/4/24 0:05
 */
@Slf4j
@RestController
public class TestController {

    @Autowired
    private TestServiceRequest testServiceRequest;
    @Autowired
    private SessionManager sessionManager;
    @Autowired
    private ServerSender serverSender;
    @Autowired
    private ServerSendTemplate serverSendTemplate;

    @RequestMapping("/hello/server")
    public MessageModule server(MessageModule module){
        MessageModule hello = testServiceRequest.hello(module);
        log.info("接收到服务端的响应 {}", hello);
        return hello;
    }

    @RequestMapping("/hello/client")
    public MessageModule client(MessageModule module){
        Collection<Session> sessions = sessionManager.findByModuleId(module.getModuleId());
        Session session = Lists.newArrayList(sessions).get(0);
        return serverSendTemplate.sendToClient(session, "/test/client1", module, MessageModule.class);
    }
}
